﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EIIS.Infrastructure.Service.Business;
using EIIS.Infrastructure.Service.System;
using EIIS.Infrastructure.Model.Business;
using EIIS.Infrastructure.Model.System;
using EIIS.Presentation.Controllers.CommonSupport;
using EIIS.Presentation.Controllers.ViewModel;
using EIIS.Common.Jsons;
using EIIS.FrameWork.Attributes;
using EIIS.Infrastructure.Model.System.Authority;

namespace EIIS.Presentation.Controllers.Business
{
    /// <summary>
    /// 包的控制器:回收部分
    /// </summary>
    public partial class PackageController : BaseController
    {
        [Description("回收管理页面")]
        [ViewPage]
        public ActionResult HIndex(string id)
        {
            return View();
        }

        [Description("回收列表页面")]
        [ViewPage]
        public ActionResult HList(string id)
        {
            return View();
        }

        public ActionResult HLoadAllByPage(string id, int page, int rows, string order, string sort)
        {
            long total = 0;
            IList<Package> ilist = GetPackages(id, page, rows, order, sort, out total).OrderByDescending(p => p.HuiSSJ).ToList();
            var list = ilist.Select(p => new
            {
                p.Id,
                p.FaFR,
                p.TiaoMH,
                p.ZhuangT,
                p.HuiSR,
                FaFRQ = string.IsNullOrEmpty(p.FaFSJ) == true ? string.Empty : DateTime.Parse(p.FaFSJ).ToString("yyyy-MM-dd"),
                FaFSJ = string.IsNullOrEmpty(p.FaFSJ) == true ? string.Empty : DateTime.Parse(p.FaFSJ).ToString("HH:mm:ss"),
                HuiSRQ = string.IsNullOrEmpty(p.HuiSSJ) == true ? string.Empty : DateTime.Parse(p.HuiSSJ).ToString("yyyy-MM-dd"),
                HuiSSJ = string.IsNullOrEmpty(p.HuiSSJ) == true ? string.Empty : DateTime.Parse(p.HuiSSJ).ToString("HH:mm:ss"),
                BaoMC = DictionaryManager.Get(p.BaoMC) == null ? "" : DictionaryManager.Get(p.BaoMC).Name,
                LingYKS = DictionaryManager.Get(p.LingYKS) == null ? "" : DictionaryManager.Get(p.LingYKS).Name,
                LingYR = DictionaryManager.Get(p.LingYR) == null ? "" : DictionaryManager.Get(p.LingYR).Name,
                ShiYR = DictionaryManager.Get(p.ShiYR) == null ? "" : DictionaryManager.Get(p.ShiYR).Name
            });
            return this.JsonFormat(new GridData { total = total, rows = list.ToList() });
        }

        [Description("新增回收")]
        [Function(ButtonID = "btnAdd")]
        public ActionResult HAdd()
        {
            this.ViewData["entity"] = new Package();
            return View();
        }

        [Description("确认回收")]
        [Function(ButtonID = "btnSave")]
        public ActionResult HSave()
        {
            MessageInfo msg = new MessageInfo();

            long count = PackageManager.CountByZhuangT(40);
            if (count > 0)
            {
                if (Cache.GetSessionCache("HospUser") != null)
                {
                    HospUser user = (HospUser)Cache.GetSessionCache("HospUser");
                    if (PackageManager.SaveHuiS(user.Name))
                    {
                        msg.IsSuccess = true;
                        msg.Message = "回收成功";
                        return Json(msg);
                    }
                    else
                    {
                        msg.IsSuccess = false;
                        msg.Message = "回收失败!";
                        return Json(msg);
                    }
                }
                else
                {
                    msg.IsSuccess = false;
                    msg.Message = "获取回收人信息失败";
                    return Json(msg);
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Message = "没有可回收的包！";
                return Json(msg);
            }                        
        }

        //获取发放的包名称
        public ActionResult GetFBaoMC(string id)
        {
            Dictionary dictionary = DictionaryManager.GetByTiaoMH(id, 3);
            if (dictionary == null)
            {
                dictionary = new Dictionary();
                dictionary.Id = string.Empty;
                dictionary.Name = string.Empty;
            }
            else
            {
                var model = PackageManager.GetByTiaoMH(id);
                model.ZhuangT = 40;
                PackageManager.Update(model);
            }
            return this.JsonFormat(new { id = dictionary.Id, text = dictionary.Name });
        }
    }
}
